package cn.qhuan.modules.controller;

import cn.qhuan.modules.dtos.SpecialDto;
import cn.qhuan.modules.dtos.SpecialShowDto;
import cn.qhuan.modules.dtos.SpecialTimeDto;
import cn.qhuan.modules.entity.Special;
import cn.qhuan.modules.service.ISpecialService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
* @Description: 专题表
* @Author: 司翔宇
* @Date:   2020-12-22
* @Version: V1.0
*/
@Api(tags="专题表")
@RestController
@RequestMapping("/special")
@Slf4j
public class SpecialController extends JeecgController<Special, ISpecialService> {
   @Autowired
   private ISpecialService specialService;

    /**
     * 通过条件分页查询
     * @param specialDto
     * @return
     */
    @AutoLog(value = "专题表-通过条件分页查询")
    @ApiOperation(value="专题表-通过条件分页查询", notes="专题表-通过条件分页查询")
    @PostMapping(value = "/list")
    public Result<?> queryByKeyWord(@RequestBody SpecialDto specialDto){
        return specialService.findAllByKeyWord(specialDto);
    }

   /**
    *   添加
    *
    * @param qhSpecial
    * @return
    */
   @AutoLog(value = "专题表-添加")
   @ApiOperation(value="专题表-添加", notes="专题表-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody Special qhSpecial) {
       specialService.save(qhSpecial);
       return Result.OK("添加成功！");
   }

   /**
    *  编辑
    *
    * @param qhSpecial
    * @return
    */
   @AutoLog(value = "专题表-编辑")
   @ApiOperation(value="专题表-编辑", notes="专题表-编辑")
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody Special qhSpecial) {
       specialService.updateById(qhSpecial);
       return Result.OK("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "专题表-通过id删除")
   @ApiOperation(value="专题表-通过id删除", notes="专题表-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       specialService.removeById(id);
       return Result.OK("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "专题表-批量删除")
   @ApiOperation(value="专题表-批量删除", notes="专题表-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.specialService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.OK("批量删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "专题表-通过id查询")
   @ApiOperation(value="专题表-通过id查询", notes="专题表-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       Special qhSpecial = specialService.getById(id);
       if(qhSpecial==null) {
           return Result.error("未找到对应数据");
       }
       return Result.OK(qhSpecial);
   }



    /**
     * 默认加载最新发布的专题
     * @param specialTimeDto
     * @return
     */
    @AutoLog(value = "专题表-默认加载最新发布专题")
    @ApiOperation(value="专题表-默认加载最新发布专题", notes="专题表-默认加载最新发布专题")
    @PostMapping(value = "/loadNewList")
    public Result<?> LoadNewSpcByTime(@RequestBody SpecialTimeDto specialTimeDto){
        return specialService.loadNewSpecList(specialTimeDto);
    }
   /**
   * 导出excel
   *
   * @param request
   * @param qhSpecial
   */
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, Special qhSpecial) {
       return super.exportXls(request, qhSpecial, Special.class, "专题表");
   }

   /**
     * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
       return super.importExcel(request, response, Special.class);
   }
    /**
     * 前台是否显示
     * @param dto
     * @return
     */
    @AutoLog(value = "专题表-前台是否显示")
    @ApiOperation(value = "专题表-前台是否显示", notes = "专题表-前台是否显示")
    @PostMapping("/show")
    public Result showSpec(@RequestBody SpecialShowDto dto,Integer type){
        return specialService.updateShowStatus(dto,type);
    }

    /**
     * 根据专题id查询关联的作品
     * @param dto
     * @return
     */
    @AutoLog(value = "专题表-根据专题id查询关联的作品")
    @ApiOperation(value = "专题表-根据专题id查询关联的作品", notes = "专题表-根据专题id查询关联的作品")
    @PostMapping("/findListBySpecialId")
    public Result findListBySpecialId(@RequestBody SpecialDto dto){
        return specialService.findWorksListBySpecialId(dto);
    }
}
